//
//  CacheData.m
//  YueMi
//
//  Created by Rainbow on 4/27/11.
//  Copyright 2011 iTotemStudio. All rights reserved.
//

#import "CacheData.h"
#import "PersistenceData.h"

@implementation CacheData
@synthesize selectedLetter,letters, viewType,viewFrom,wrongWords,savedLetters;
@synthesize currentLetterIndex;
@synthesize currentCategory;
@synthesize needUpdateLetters;
@synthesize tempMessage;
@synthesize testYears = _testYears;
@synthesize advertising = _advertising;
static CacheData *_sharedInst;

+(CacheData *)sharedData
{
	@synchronized(self)
	{
		if (_sharedInst == nil) {
			_sharedInst = [[CacheData alloc] init];
		}
	}
	return _sharedInst;
}	

-(id)init
{
    self = [super init];
    if (self) {
        self.letters = [NSArray arrayWithObjects:@"A",@"B",@"C",@"D",@"E",@"F",@"G",@"H",@"I",@"J",@"K",@"L",@"M",@"N",@"O",@"P",@"Q",@"R",@"S",@"T",@"U",@"V",@"W",@"X",@"Y",@"Z",MORE_WORDS , nil];
        wrongWords = [[NSMutableArray alloc] init];
        savedLetters = [[NSMutableDictionary alloc] init];
        needUpdateLetters = [[NSMutableArray alloc] init];
        
        _advertising = [(Advertising *)[NSKeyedUnarchiver unarchiveObjectWithData:[USER_DEFAULT objectForKey:AD_CACHE_KEY]] retain];
        if (!_advertising)
        {
            _advertising = [[Advertising alloc] init];
        }
    }
    return self;
}

- (void)setAdvertising:(Advertising *)advertising
{
    if (_advertising)
    {
        RELEASE_SAFELY(_advertising);
    }
    _advertising = [advertising retain];
    
    [USER_DEFAULT setObject:[NSKeyedArchiver archivedDataWithRootObject:_advertising] forKey:AD_CACHE_KEY];

}

- (void)setCurrentCategory:(NSString *)_currentCategory
{
    if (currentCategory != nil) {
        [currentCategory release];
        currentCategory = nil;
    }
    [[CacheData sharedData].needUpdateLetters removeAllObjects];
    self.selectedLetter = nil;
    currentCategory = [_currentCategory retain];
    if ([[PersistenceData sharedData] getWrongWords] != nil) {
        
        self.wrongWords = [[PersistenceData sharedData] getWrongWords];
    }
    if ([[PersistenceData sharedData] getSavedLetters] != nil) {
        
        self.savedLetters = [[PersistenceData sharedData] getSavedLetters];
    }
}

- (void) loadSelectedLetter:(NSInteger )letterIndex withFrom:(HotWordViewFrom)from
{
    if (letterIndex < 0 || letterIndex >= [self.letters count]) {
        return;
    }

    [self syncLetterStatus];
    currentLetterIndex = letterIndex;
    self.selectedLetter = [savedLetters objectForKey:[letters objectAtIndex:currentLetterIndex]];
    if (from == HotWordViewFromBeginning) {
        if (viewType == HotWordViewTypeLearn) {
            self.selectedLetter.learnIndex = [NSNumber numberWithInt:0];
        }
        else
        {
            self.selectedLetter.practiceIndex = [NSNumber numberWithInt:0];
        }
    }
    //wenbin 更新    9.11
}

- (BOOL) loadPerviousLetter
{
    NSInteger letterIndex = currentLetterIndex - 1;
    if (letterIndex < 0 || letterIndex >= [self.letters count]) {
        return NO;
    }
    [self syncLetterStatus];
    currentLetterIndex = letterIndex;
    self.selectedLetter = [savedLetters objectForKey:[letters objectAtIndex:currentLetterIndex]];
    return YES;
}

- (BOOL) loadNextLetter
{
    NSInteger letterIndex = currentLetterIndex + 1;
    if (letterIndex < 0 || letterIndex >= [self.letters count]) {
        return NO;
    }
    [self syncLetterStatus];
    currentLetterIndex = letterIndex;
    self.selectedLetter = [savedLetters objectForKey:[letters objectAtIndex:currentLetterIndex]];
    return YES;
    
}

- (Word *)getWordByPosition:(HotWordGetWordPosition)position;
{
    if (self.viewType == HotWordViewTypeLearn) {
        switch (position) {
            case HotWordGetWordPositionFirst:
                self.selectedLetter.learnIndex = [NSNumber numberWithInt:0];
                break;
                
            case HotWordGetWordPositionNext:
            {
                NSInteger index = [self.selectedLetter.learnIndex intValue];
                if (index + 1 >= [self.selectedLetter.words count]) {
//                    if (currentLetterIndex + 1 < [self.letters count]) {
//                        [self loadSelectedLetter:currentLetterIndex + 1 withFrom:HotWordViewFromBeginning];
//                    }
                    return nil;
                }
                else
                {
                    self.selectedLetter.learnIndex = [NSNumber numberWithInt:index + 1];
                }
            }
                break;
                
            case  HotWordGetWordPositionPre:
            {
                NSInteger index = [self.selectedLetter.learnIndex intValue];
                if (index - 1 < 0) {
//                    if (currentLetterIndex - 1 >= 0) {
//                        [self loadSelectedLetter:currentLetterIndex - 1 withFrom:HotWordViewFromLastTime];
//                        self.selectedLetter.learnIndex = [NSNumber numberWithInt:[self.selectedLetter.words count] - 1];
//                        
//                    }
                    return nil;
                }
            }
                break;
                
            default:
                break;
        }
        return [self.selectedLetter.words objectAtIndex:[self.selectedLetter.learnIndex intValue]];

    }
    else
    {
        switch (position) {
            case HotWordGetWordPositionFirst:
                self.selectedLetter.practiceIndex = [NSNumber numberWithInt:0];
                break;
                
            case HotWordGetWordPositionNext:
            {
                NSInteger index = [self.selectedLetter.practiceIndex intValue];
                if (index + 1 >= [self.selectedLetter.words count]) {
//                    if (currentLetterIndex + 1 < [self.letters count]) {
//                        [self loadSelectedLetter:currentLetterIndex + 1 withFrom:HotWordViewFromBeginning];
//                    }
                    return nil;
                }
                else
                {
                    self.selectedLetter.practiceIndex = [NSNumber numberWithInt:index + 1];
                }
            }
                break;
                
            case  HotWordGetWordPositionPre:
            {
                NSInteger index = [self.selectedLetter.practiceIndex intValue];
                if (index - 1 < 0) {
                    if (currentLetterIndex - 1 >= 0) {
//                        [self loadSelectedLetter:currentLetterIndex - 1 withFrom:HotWordViewFromLastTime];
//                        self.selectedLetter.practiceIndex = [NSNumber numberWithInt:[self.selectedLetter.words count] - 1];
                        return nil;
                    }
                }
            }
                break;
                
            default:
                break;
        }
        return [self.selectedLetter.words objectAtIndex:[self.selectedLetter.practiceIndex intValue]];
    }
    
}

- (Word *)getWordByIndex:(NSInteger)index
{
    if (self.viewType == HotWordViewTypeLearn) {
        if (index >= 0 || index < [letters count]) {
            self.selectedLetter.learnIndex = [NSNumber numberWithInt:index];
        }
        return [self.selectedLetter.words objectAtIndex:[self.selectedLetter.learnIndex intValue]];
        
    }
    else
    {      
        if (index >= 0 || index < [letters count]) {
            self.selectedLetter.practiceIndex = [NSNumber numberWithInt:index];
        }
        return [self.selectedLetter.words objectAtIndex:[self.selectedLetter.practiceIndex intValue]];
    }
}

- (void)syncLetterStatus
{
    if (self.selectedLetter != nil) {
        
        [[CacheData sharedData].savedLetters setValue:self.selectedLetter forKey:self.selectedLetter.letter];
    }
}

- (void)addWrongWord:(Word *)word
{
    for (Word *existWord in wrongWords) {
        if ([word.content isEqualToString:existWord.content]) {
            return;
        }
    }
    
    if (word != nil) {
        
        [wrongWords addObject:word];
    }
    [[PersistenceData sharedData] syncWrongWords:wrongWords];
}

- (void)removeWrongWord:(Word *)word
{
    Word *tempWord = nil;
    for (Word *wrongWord in wrongWords) {
        if ([word.content isEqualToString:wrongWord.content]) {
            tempWord = wrongWord;
            break;
        }
    }
    [wrongWords removeObject:tempWord];
    [[PersistenceData sharedData] syncWrongWords:wrongWords];
}

- (NSUInteger)retainCount{
	return NSUIntegerMax;
}

- (oneway void)release{
}

- (id)retain{
	return _sharedInst;
}

- (id)autorelease{
	return _sharedInst;
}

- (void) restore{
}

- (NSArray *)testYears
{
    if (_testYears)
    {
        if ([_testYears count] > 0)
        {
            return _testYears;
        }
    }
    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy"];
    NSString *currentYearStr = [dateFormatter stringFromDate:[NSDate date]];
    [dateFormatter release];
    NSMutableArray *years = [NSMutableArray array];
    [years addObject:currentYearStr];
    
    NSInteger currentYear = [currentYearStr integerValue];
    for (int i = 0; i < 2; i++)
    {
        currentYear++;
        [years addObject:[NSString stringWithFormat:@"%d",currentYear]];
    }
    
    _testYears = [[NSArray alloc] initWithArray:years];
    
    return _testYears;
}

-(void)dealloc
{
    self.tempMessage = nil;
    self.savedLetters = nil;
    self.letters = nil;
    self.selectedLetter = nil;
    self.currentCategory = nil;
    self.needUpdateLetters = nil;
    self.testYears = nil;
    self.advertising = nil;
    [super dealloc];
}

@end
